<template>
  <CrudTable
    :buttons="[]"
    row-selection-type="multiple"
    :columns="tableColumns"
    @currentChange="currentChange"
    :tableData="pageData.tableData.records"
    :loading="pageData.loading"
    :pagination="pageData.pagination"
    @page-change="handlePageChange"
    table-max-height="75vh"
    :showRefresh="true"
    @refreshData="handleTableRefresh"
  />
</template>

<script lang="tsx" setup>
import { ref, reactive } from "vue";
import { type TableProps } from "tdesign-vue-next";

import { http } from "@/utils/fetch";

import type { ICrudTableDataPage } from "@/types";
import { PAGINATION_VALUE } from "@/common/constants";
// import EditForm from "./components/EditForm.vue";

const GET_LIST_PATH = "/log/findAll"; // 查询信息接口
// const DELETE_ITEM_PATH = ""; // 删除接口

const pageData = reactive({
  loading: false,
  tableData: {} as ICrudTableDataPage,
  selectedRow: {},
  selectedRowId: "",
  showEditForm: false,
  editFormMode: "",
  pagination: {
    pageSize: 20,
    total: 0,
    current: 1
  }
});

// 表格列配置
const tableColumns = ref<TableProps["columns"]>([
  {
    colKey: "operationName",
    title: "操作名称",
    width: 400
  },
  {
    colKey: "operationType",
    title: "操作类型"
  }
]);

// 获取列表数据
const findAllList = async () => {
  pageData.loading = true;
  const resultData = await http(
    "GET",
    `${GET_LIST_PATH}?currentPage=${pageData.pagination.current}&pageSize=${pageData.pagination.pageSize}`
  );
  if (resultData.code === 200) {
    pageData.tableData = resultData.data;
    pageData.pagination.total = resultData.data.total;
    pageData.pagination.current = resultData.data.current;
    pageData.pagination.pageSize = resultData.data.size;
    pageData.loading = false;
  }
};

// 当行选择发生变化时的处理函数
const currentChange = (e: Record<string, any>) => {
  const rowData = e.options.currentRowData;
  pageData.selectedRow = rowData;
  pageData.selectedRowId = rowData.id;
};

const handlePageChange = (pageInfo: Record<string, any>, newDataSource: Record<string, any>) => {
  console.log(pageInfo, newDataSource);
  pageData.pagination.pageSize = pageInfo.pageSize;
  pageData.pagination.current = pageInfo.current;
  findAllList();
};

const handleTableRefresh = () => {
  pageData.selectedRow = {};
  pageData.selectedRowId = "";
  pageData.pagination = structuredClone(PAGINATION_VALUE);
  findAllList();
};

// 过滤事件触发函数
// const filterChange = (filters: Record<string, any>) => {
//   pageData.filterValue = filters;
//   pageData.pagination = {
//     pageSize: 20,
//     total: 0,
//     current: 1
//   };
//   findAllList();
// };

findAllList();
</script>
